#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/10/11 13:49
# @Author  : ystraw
# @Site    : 
# @File    : DealWordFile.py
# @Software: PyCharm Community Edition
# @function: 整理单词文件

def readWordFile(filename):
    with open(filename, 'r', encoding='utf-8') as f:
        file = f.read()
    return file

def writeWordFile(filename, filecontent):
    with open(filename, 'w+', encoding='utf-8') as f:
        f.write(filecontent)

if __name__ == '__main__':
    # 读入原始文件，进行处理
    words = readWordFile('words/960words.txt')
    # print(words)
    # print(len(words.split('\n')))
    wordslist = words.split('\n')
    sta = wordslist[0]
    end = wordslist[2]
    cnt = 1
    newwords = sta
    for i in range(1, len(wordslist) - 1):
        sta = wordslist[i-1]
        end = wordslist[i+1]
        word = wordslist[i]
        if (not sta is None) and (word[0] != sta[0] or word[0] != end[0]):
            # 提取 n, verb, adj, prep,
            if word[0] == 'n'and (len(word) == 1 or (len(word) > 1 and word[1] == ' ')):
                newwords += ' ' + word
                # print('n----', i)
            elif word[0:4] == 'verb':
                newwords += ' ' + word
                # print('verb----', i)
            elif word[0:3] == 'adj':
                newwords += ' ' + word
                # print('adj----', i)
            elif word[0:4] == 'prep':
                newwords += ' ' + word
                # print('prep----', i)
            else:
                newwords += '\n' + word
                cnt += 1
        else:
            newwords += '\n' + word
            cnt += 1

    newwords += '\n' + end
    print('end: ', cnt)
    writeWordFile('words/960words_new.txt', newwords)

    # 读入处理后文件，进行分割
    words = readWordFile('words/960words_new.txt')
    wordslist = words.split('\n')
    newwords = ''
    for word in wordslist:
        for i in range(0, len(word)):
            if not((word[i] >= 'a' and word[i] <= 'z') or  (word[i] >= 'A' and word[i] <= 'Z') ):
                word = word[0:i] +  '#' + word[i+1:]
                break
        for j in range(0, len(word)):
            i = len(word) - j - 1
            if not((word[i] >= 'a' and word[i] <= 'z') or  (word[i] >= 'A' and word[i] <= 'Z') ):
                word = word[0:i] +  '#' + word[i+1:]
                break
        print(word)
        newwords += word + '\n'
        # break
    writeWordFile('words/960words_new2.txt', newwords)